package com.infinitus.autocatlog.task;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;





import com.infinitus.autocatlog.domain.RecordItem;
import com.infinitus.autocatlog.service.util.TimeUtil;


public class FilterRules {

	
	private static Pattern pattern = Pattern.compile("\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");

	private static List<String> doTypes = new ArrayList<String>();
	static{
		String[] types = {"浏览","点击","对焦","启动","退出","唤醒","后台","停留","安装"};
		Collections.addAll(doTypes, types);
	}
	
	/**
	 * 对 Session_ID  Client_Device_ID  System_Name 处理
	 * @param str
	 * @return
	 */
	public static boolean isNotBlank(String str){
		if(str != null && !"".equals(str.trim())){
			return true;
		}
		return false;
	}
	
	public static boolean checkIP(String str){
        return pattern.matcher(str).matches();
	}
	
	/**
	 * 时间过滤
	 * Event_DateTime >= 现在 – 7天 AND Event_DateTime <= 现在
	 * @param datetime
	 * @return
	 * @throws ParseException 
	 */
	public static boolean checkEventDate(String datetime){
		try {
			Date eventTime = new Date(Long.parseLong(datetime));
			
			Date now = new Date();
			if(eventTime.after(now)){
				return false;
			}
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(now);
			calendar.add(Calendar.DATE, -7);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			Date expiryDate = calendar.getTime();
			
			if(eventTime.before(expiryDate)){
				return false;
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 只可以是“浏览” OR “点击” OR “对焦” OR “启动” OR “退出” OR “唤醒” OR “后台”
	 * @param str
	 * @return
	 */
	public static boolean checkEventAction(String type){
		if(doTypes.contains(type)){
			return true;
		}
		return false;
	}
	
	/**
	 * 如Event_Label是”点击” OR “对焦“,非空白
	 * @param str
	 * @return
	 */
	public static boolean checkEventLabel(String action, String label){
		if((action.equals("点击") || action.equals("对焦")) && !isNotBlank(label)){
			return false;
		}
		return true;
	}
	
	
	public static String checkAll(RecordItem one, String osplat){
		if(!isNotBlank(one.getApp_name())){
			one.setApp_name("null");
			return "systemName_null_error";
		}
		if(!checkEventDate(one.getEvent_time())) return "eventDateTime_error";
		if(!isNotBlank(one.getPage_name())) return "eventScreen_null_error";
		if(!isNotBlank(one.getEvent_name())) return "label_null_error";
		if(!checkEventAction(one.getEvent_type())) return "action_error";
		if(!checkEventLabel(one.getEvent_type(), one.getEvent_name())) return "eventLable_error";
		
		if(!isNotBlank(one.getDistinct_id())) return "distinct_id_null_error";
		if(!isNotBlank(one.getSession_id())) return "sesionID_null_error";
//		if(!checkIP(one.getSugo_ip())) return "ip_error";
		
		if("app".equals(osplat)){
			if(!isNotBlank(one.getDevice_id())) return "clientDeviceID_null_error";
		}
		return null;
	}
	
	public static void main(String[] args){
		String s = "<td valign=\"middle\" align=\"center\" height=\"40\">水质<em style='color:red'>测试</em>售价 ";
		s= s.replaceFirst("[^<]*>", "").replaceAll("<((?!/?em).).*?>?", "");
		System.out.println(s);
		String[] sarr = s.split("");
		int sliceSize = 10;
		boolean isTagCxt = false;
		int index=0;
		while( index < sarr.length) {
			if("<".equals(sarr[index])){
				isTagCxt= true;
			}
			if(!isTagCxt){
				sliceSize--;
			}
			if(">".equals(sarr[index])){
				isTagCxt= false;
			}
			index++;
			if(sliceSize<=0){
				break;
			}
		}
		index = index>s.length()?s.length():index;
		s = s.substring(0, index);
		if(isTagCxt){
			s = s.substring(0,s.lastIndexOf("<"));
		}
		System.out.println(s);
		
		List<String> ls = new ArrayList<String>();
		Pattern p = Pattern.compile("<.+?>");
		Matcher m = p.matcher(s);
		while(m.find()){
			ls.add(m.group());
		}
		if(ls.size()>0){
			if(ls.get(0).matches("^</em.*?>$")){
				s ="<em>"+s;
			}
			if(ls.get(ls.size()-1).matches("^<em.*?>$")){
				s+="</em>";
			}
		}
		System.out.println(s);
		/*Pattern compile = Pattern.compile("[\u4E00-\u9FFF]");
		Matcher matcher = compile.matcher(s);
		int start =0;
		int end =0;
		int i=10;
		while(matcher.find()){
			if(i==0){
				end = matcher.start()-1;
				break;
			}else if(i==10){
				start = matcher.start();
			}
			i--;
		}
		String res = s.substring(start , end);
		//补后标签
		String postfix = s.substring(end);
		if(!"".equals(postfix)){
			Pattern p = Pattern.compile("</.+?>");
			Matcher m = p.matcher(postfix);
			if(m.find()){
				res = res+m.group();
			}
		}
		//补前标签
		String prefix = s.substring(0, start);
		if(!"".equals(res)){
			Pattern p1 = Pattern.compile("</.+?>");
			Matcher m1 = p1.matcher(res);
			int cnt=0;
			while(m1.find()){
				cnt++;
			}
			Stack<String> st = new Stack<String>();
			Pattern p2 = Pattern.compile("<.+?>");
			Matcher m2 = p2.matcher(prefix);
			while(m2.find()){
				st.push(m2.group());
			}
			for (int j = 0; j < cnt; j++) {
				if(!st.isEmpty()){
					res = st.pop()+res;
				}
			}
			System.out.println(res);
		}*/
	}
}
